import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-10-24
 * Time: 20:08
 */
public class MybitSet {
    public byte[] elem;
    public int usedSize;
    public MybitSet(){
        elem = new byte[1];
    }
    public MybitSet(int n){
        //不能被8整除的时候开一个，以便能放下17这种数字
        elem = new byte[n / 8 + 1];
    }
    public void set(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("下标越界!");
        }
        int arrIndex = val / 8;
        if(arrIndex > elem.length - 1){
            elem = Arrays.copyOf(elem, arrIndex + 1);
        }
        int bitIndex = val % 8;
        elem[arrIndex] |= (1 << bitIndex);
        usedSize++;
    }
    public boolean get(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("下标越界");
        }
        int arrIndex = val / 8;
        if(arrIndex > elem.length - 1){
            throw new IndexOutOfBoundsException("下标越界");
        }
        int bitIndex = val % 8;
        if(((elem[arrIndex]) & ( 1 << bitIndex)) != 0){
            return true;
        }
        return false;
    }
    public void reset(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("下标越界");
        }
        int arrIndex = val / 8;
        if(arrIndex > elem.length - 1){
            throw new IndexOutOfBoundsException("下标越界");
        }
        int bitIndex = val % 8;
        elem[arrIndex] &= ~(1 << bitIndex);
        usedSize--;
    }
    public int getUsedSize(){
        return usedSize;
    }

    public static void main(String[] args) {
        MybitSet mybitSet = new MybitSet(20);
        int[] arr = new int[]{1,3, 7, 20, 23};
        for (int i = 0; i < arr.length; i++) {
            mybitSet.set(arr[i]);
        }
        for (int i = 0; i < arr.length; i++) {
            boolean ret = mybitSet.get(arr[i]);
            System.out.println(ret);
        }
        mybitSet.reset(23);
        System.out.println(mybitSet.get(23));
    }
}
